//
// C++ Implementation: threadstablewidget
//
// Description: 
//
//
// Author: Christophe Vandeplas <christophe@vandeplas.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#ifdef USE_LOADGEN_GUI

#include <iostream>
//#include <QApplication>
#include <QString>
#include "threadstablewidget.h"


using std::cout;
using std::endl;


ThreadsTableWidget::ThreadsTableWidget(QWidget *parent)
 : QTableWidget(parent)
{
    _lastAddedItemId = 0;

    QTableWidgetItem *__colItem0 = new QTableWidgetItem();
    __colItem0->setText("Status");
    this->setHorizontalHeaderItem(1, __colItem0);

    QTableWidgetItem *__colItem1 = new QTableWidgetItem();
    __colItem1->setText("Loop");
    this->setHorizontalHeaderItem(1, __colItem1);

    QTableWidgetItem *__colItem2 = new QTableWidgetItem();
    __colItem2->setText("Scene file");
    this->setHorizontalHeaderItem(2, __colItem2);

}


ThreadsTableWidget::~ThreadsTableWidget()
{
}


void ThreadsTableWidget::addNewThreadToTable( int numOfRepeats, QString scenefile, bool fitToContent )
{
    //cout << "Adding thread to table: " <<numOfRepeats <<  endl;
    QString repeatsString;
    repeatsString.append("1").append("/").append(QString::number(numOfRepeats));
    insertRow(rowCount());                                                  // adds new row at end of table
    setItem( _lastAddedItemId , 0, new QTableWidgetItem("loaded") );         // 1st column
    setItem( _lastAddedItemId , 1, new QTableWidgetItem(repeatsString) );   // 2nd column
    setItem( _lastAddedItemId , 2, new QTableWidgetItem(scenefile) );       // 3rd column
    _lastAddedItemId++;
    //repaint();
    update();
    if (fitToContent) {
        resizeColumnsToContents ();
        resizeRowsToContents ();
    }


}

void ThreadsTableWidget::updateThreadOfTable( int number, int loop, QString state )
{
    item(number,0)->setText(state);                                         // 1st column
    QString repeatsString(item(number,1)->text());
    int slashIndex= repeatsString.indexOf('/');
    repeatsString.replace(0, slashIndex, QString::number(loop));
    item(number,1)->setText(repeatsString);                                 // 2nd column
    repaint();
    //resizeColumnsToContents ();

}

void ThreadsTableWidget::cleanUpTable()
{
    clear();
    setRowCount(0);
    _lastAddedItemId=0;
    repaint();
}

void ThreadsTableWidget::cellClicked( int row, int col )
{
    emit ( updateListOfElements ( row , true ) );
    //emit ( changeAudioThread (row) );
    //std::cerr << "emit ( changeAudioChannel (row) );" <<endl;
}


#endif // USE_LOADGEN_GUI
